package cn.itsource.hrm.service.impl;

import cn.hutool.core.util.ZipUtil;
import cn.itsource.hrm.Client.CourseClient;
import cn.itsource.hrm.PageClient;
import cn.itsource.hrm.client.FileClient;
import cn.itsource.hrm.controller.vo.CourseVo;
import cn.itsource.hrm.doc.CourseDoc;
import cn.itsource.hrm.domain.Course;
import cn.itsource.hrm.domain.CourseDetail;
import cn.itsource.hrm.domain.CourseMarket;
import cn.itsource.hrm.mapper.CourseDetailMapper;
import cn.itsource.hrm.mapper.CourseMapper;
import cn.itsource.hrm.mapper.CourseMarketMapper;
import cn.itsource.hrm.query.CourseQuery;
import cn.itsource.hrm.service.ICourseDetailService;
import cn.itsource.hrm.service.ICourseService;
import cn.itsource.hrm.service.ICourseTypeService;
import cn.itsource.util.AjaxResult;
import cn.itsource.util.PageList;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenyuqi
 * @since 2020-05-24
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {
    @Autowired
    private CourseDetailMapper courseDetailMapper;
    @Autowired
    private CourseClient courseClient;
    @Autowired
    private CourseMarketMapper courseMarketMapper;
    @Autowired
    private FileClient fileClient;
    @Autowired
    private PageClient pageClient;

    /**
     * 分页高级查询
     * @param query
     * @return
     */
    @Override
    public PageList<Course> queryPage(CourseQuery query) {
        IPage<Course> ipag = baseMapper.selectPage(new Page<Course>(query.getPage(), query.getRows()), query);
        PageList<Course> pageList = new PageList<>(ipag.getTotal(),ipag.getRecords());
        return pageList;
    }

    @Override
    public void updateById(CourseVo courseVo) {

    }

    /**
     *
     * @param courseVo
     */
    @Override
    @Transactional
    public void save(CourseVo courseVo) {
        Course course = new Course();
        BeanUtils.copyProperties(courseVo,course);

        course.setTenantId(1L);
        course.setTenantName("测试002");
        course.setUserId(1L);
        course.setUserName("ceshi");
        course.setStatus(0);

        baseMapper.insert(course);

        CourseDetail courseDetail = new CourseDetail();
        courseDetail.setCourseId(course.getId());
        courseDetail.setIntro(courseVo.getIntro());
        courseDetail.setDescription(courseVo.getDescription());
        courseDetailMapper.insert(courseDetail);

    }

    /**
     * 上线课程
     * @param ids
     */
    @Override
    public void online(List<Long> ids) {
        //先从数据库查询上线的课程信息
        List<Course> courseList = baseMapper.selectBatchIds(ids);

        List<CourseDoc> courseDocList = courseToDocs(courseList);

        //保存到es中
        courseClient.saveAll(courseDocList);
        //修改数据库中的状态和上线时间
        baseMapper.online(ids,System.currentTimeMillis());
    }

    @Override
    public void offline(List<Long> ids) {
        //从es中删除
        courseClient.deteleAll(ids);
        //修改数据库中的状态和下线时间
        baseMapper.offline(ids,System.currentTimeMillis());
    }




    /**
     * 集合转换
     * @param courseList
     * @return
     */
    private List<CourseDoc> courseToDocs(List<Course> courseList) {
        ArrayList<CourseDoc> list = new ArrayList<>();
        for (Course course:courseList) {
            list.add(courseToDoc(course));
        }
        return list;
    }

    /**
     * 对象转换
     * @param course
     * @return
     */
    private CourseDoc courseToDoc(Course course) {
        CourseDoc courseDoc = new CourseDoc();
        BeanUtils.copyProperties(course,courseDoc);
        courseDoc.setStartTime(System.currentTimeMillis());
        CourseMarket courseMarket = courseMarketMapper.selectOne(new QueryWrapper<CourseMarket>().eq("course_Id", course.getId()));
        if (courseMarket!=null){
            BeanUtils.copyProperties(courseMarket,courseDoc,"id");
        }

        return courseDoc;
    }


}
